package com.shrct.businessprocess.customerservice.impl;

import com.shrct.businessprocess.customerservice.RCRoutineServicePlan;
import com.shrct.businessprocess.customerservice.RCRoutineServiceWorkItem;
import com.shrct.businessprocess.customerservice.RCServiceRecord;
import com.shrct.businessprocess.customerservice.RCWorkItemStatus;
import com.shrct.businessprocess.customerservice.impl.RCRoutineServicePlanImpl;
import com.shrct.businessprocess.customerservice.impl.RCServiceRecordImpl;
import com.shrct.businessprocess.customerservice.impl.RCWorkItemImpl;
import com.shrct.businessprocess.customerservice.proxy.RCRoutineServiceWorkItemProxy;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCROUTINESERVICEWORKITEM_TYPE")
public class RCRoutineServiceWorkItemImpl extends RCWorkItemImpl implements RCRoutineServiceWorkItem {
  public RCRoutineServiceWorkItemImpl() {
    super();
  }
  
  @JoinColumn(name = "plan_id")
  @ManyToOne(targetEntity = RCRoutineServicePlanImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCRoutineServicePlan plan;
  
  public RCRoutineServicePlan getPlan() {
    return this.plan;
  }
  
  public RCRoutineServiceWorkItem setPlan(final RCRoutineServicePlan plan) {
    this.plan = plan;
    return this;			
    
  }
  
  @ManyToMany(targetEntity = RCEmployeeImpl.class)
  @JoinTable(name = "m2m_rcroutineserviceworkitem_workers", joinColumns = @JoinColumn(name = "rcroutineserviceworkitem_id") , inverseJoinColumns = @JoinColumn(name = "rCEmployee_inv_id") )
  private Collection<RCEmployee> workers = new java.util.ArrayList<com.shrct.businessprocess.organization.RCEmployee>();;
  
  public Collection<RCEmployee> getWorkers() {
    return this.workers;
  }
  
  public RCRoutineServiceWorkItem setWorkers(final Iterable<RCEmployee> workers) {
    removeAllWorkers();
    for(RCEmployee ca : workers){
    	addtoWorkers(ca);
    }
    return this;		
    
  }
  
  public RCRoutineServiceWorkItem addtoWorkers(final RCEmployee rCEmployee) {
    this.workers.add(rCEmployee);
    return this;
    
  }
  
  public RCRoutineServiceWorkItem removeAllWorkers() {
    this.workers.clear();
    return this;	
    
  }
  
  public RCRoutineServiceWorkItem removeFromWorkers(final RCEmployee rCEmployee) {
    this.workers.remove(rCEmployee);
    return this;
    
  }
  
  private RCWorkItemStatus status = RCWorkItemStatus.executing;
  
  public RCWorkItemStatus getStatus() {
    return this.status;
  }
  
  public RCRoutineServiceWorkItem setStatus(final RCWorkItemStatus status) {
    this.status = status;
    return this;			
    
  }
  
  private Date planDate;
  
  public Date getPlanDate() {
    return this.planDate;
  }
  
  public RCRoutineServiceWorkItem setPlanDate(final Date planDate) {
    this.planDate = planDate;
    return this;			
    
  }
  
  private Date assignedDate;
  
  public Date getAssignedDate() {
    return this.assignedDate;
  }
  
  public RCRoutineServiceWorkItem setAssignedDate(final Date assignedDate) {
    this.assignedDate = assignedDate;
    return this;			
    
  }
  
  private Date executeDate;
  
  public Date getExecuteDate() {
    return this.executeDate;
  }
  
  public RCRoutineServiceWorkItem setExecuteDate(final Date executeDate) {
    this.executeDate = executeDate;
    return this;			
    
  }
  
  private Boolean isReported;
  
  public Boolean getIsReported() {
    return this.isReported;
  }
  
  public RCRoutineServiceWorkItem setIsReported(final Boolean isReported) {
    this.isReported = isReported;
    return this;			
    
  }
  
  private Date reportSubmitDate;
  
  public Date getReportSubmitDate() {
    return this.reportSubmitDate;
  }
  
  public RCRoutineServiceWorkItem setReportSubmitDate(final Date reportSubmitDate) {
    this.reportSubmitDate = reportSubmitDate;
    return this;			
    
  }
  
  private Date finishDate;
  
  public Date getFinishDate() {
    return this.finishDate;
  }
  
  public RCRoutineServiceWorkItem setFinishDate(final Date finishDate) {
    this.finishDate = finishDate;
    return this;			
    
  }
  
  private String comment;
  
  public String getComment() {
    return this.comment;
  }
  
  public RCRoutineServiceWorkItem setComment(final String comment) {
    this.comment = comment;
    return this;			
    
  }
  
  @ManyToMany(targetEntity = RCServiceRecordImpl.class)
  @JoinTable(name = "m2m_rcroutineserviceworkitem_servicerecords", joinColumns = @JoinColumn(name = "rcroutineserviceworkitem_id") , inverseJoinColumns = @JoinColumn(name = "rCServiceRecord_inv_id") )
  private Collection<RCServiceRecord> serviceRecords = new java.util.ArrayList<com.shrct.businessprocess.customerservice.RCServiceRecord>();;
  
  public Collection<RCServiceRecord> getServiceRecords() {
    return this.serviceRecords;
  }
  
  public RCRoutineServiceWorkItem setServiceRecords(final Iterable<RCServiceRecord> serviceRecords) {
    removeAllServiceRecords();
    for(RCServiceRecord ca : serviceRecords){
    	addtoServiceRecords(ca);
    }
    return this;		
    
  }
  
  public RCRoutineServiceWorkItem addtoServiceRecords(final RCServiceRecord rCServiceRecord) {
    this.serviceRecords.add(rCServiceRecord);
    return this;
    
  }
  
  public RCRoutineServiceWorkItem removeAllServiceRecords() {
    this.serviceRecords.clear();
    return this;	
    
  }
  
  public RCRoutineServiceWorkItem removeFromServiceRecords(final RCServiceRecord rCServiceRecord) {
    this.serviceRecords.remove(rCServiceRecord);
    return this;
    
  }
  
  public void finished(final Procedure1<? super RCRoutineServiceWorkItem> preInit) {
    preInit.apply(this);
    this.plan.finished();
    this.status = RCWorkItemStatus.finished;
  }
  
  public void suspended() {
    this.status = RCWorkItemStatus.suspended;
  }
  
  public void doAddtoServiceRecords(final RCServiceRecord serviceRecord) {
    boolean _contains = this.serviceRecords.contains(serviceRecord);
    boolean _not = (!_contains);
    if (_not) {
      this.addtoServiceRecords(serviceRecord);
    }
  }
  
  public static RCRoutineServiceWorkItem create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.customerservice.RCRoutineServiceWorkItem rCRoutineServiceWorkItem = new com.shrct.businessprocess.customerservice.impl.RCRoutineServiceWorkItemImpl(
    );
    objectFactory.create(rCRoutineServiceWorkItem);
    return rCRoutineServiceWorkItem;			
    
  }
  
  public static RCRoutineServiceWorkItem createRCRoutineServiceWorkItemImpl(final IObjectFactory objectFactory, final Procedure1<RCRoutineServiceWorkItem> init) {
    com.shrct.businessprocess.customerservice.RCRoutineServiceWorkItem rCRoutineServiceWorkItem = new com.shrct.businessprocess.customerservice.impl.RCRoutineServiceWorkItemImpl(
    );
    objectFactory.create(rCRoutineServiceWorkItem);
    init.apply(rCRoutineServiceWorkItem);
    
    return rCRoutineServiceWorkItem;			
    
  }
  
  public RCRoutineServiceWorkItemProxy toProxy() {
    com.shrct.businessprocess.customerservice.proxy.RCRoutineServiceWorkItemProxy proxy = new com.shrct.businessprocess.customerservice.proxy.RCRoutineServiceWorkItemProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
